package com.asia5b.a5mall.middleware.widget

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.databinding.DialogCancelsureBinding
import com.asia5b.a5mall.middleware.base.BaseDialog

/**
 * Created by zhangyuncai on 2018/12/11.
 * 确认取消框
 */
class CancelSureDialog(context: Context, val viewModel: CancelSureViewModel, override var layoutId: Int = R.layout.dialog_cancelsure) : BaseDialog(context) {

    override var widthPercent: Double = 0.8
    override var gravity: Int = Gravity.CENTER
    var binding: DialogCancelsureBinding? = null
    override fun getDataBinding(): View? {
        binding = DialogCancelsureBinding.inflate(LayoutInflater.from(context))
        return binding?.root
    }

    override fun initView() {
        binding?.model = viewModel
    }

    override fun initEvent() {
    }


}

class CancelSureViewModel(val context: Context,

                          @StringRes
                          var leftStringRes: Int = 0
                          ,
                          @StringRes
                          var rightStringRes: Int = 0,
                          @StringRes
                          var contentStringRes: Int = 0,
                          var rightClick: () -> Unit) {
    val cancelSureDialog = CancelSureDialog(context, this)

    constructor(context: Context) : this(context, 0, 0, 0, {})

    constructor(context: Context, @StringRes contentStringRes: Int, callback: () -> Unit)
            : this(context, 0, 0, contentStringRes, callback)

    var leftClick: () -> Unit = {}

    var leftString: String? = null
        get() {
            if (leftStringRes > 0) {
                return ResString.get(leftStringRes)
            }
            if (field.isNullOrEmpty()) {
                return ResString.get(R.string.取消)
            }
            return field
        }

    @SuppressLint("ResourceAsColor")
    @ColorInt
    var leftColorInt: Int = R.color.common_text_gray
        get() {
            if (field != 0) {
                return ContextCompat.getColor(context, field)
            }
            return field
        }


    var rightString: String? = null
        get() {
            if (rightStringRes > 0) {
                return ResString.get(rightStringRes)
            }
            if (field.isNullOrEmpty()) {
                return ResString.get(R.string.确定)
            }
            return field
        }

    @SuppressLint("ResourceAsColor")
    @ColorInt
    var rihghtColorInt: Int = R.color.common_text_blue
        get() {
            if (field != 0) {
                return ContextCompat.getColor(context, field)
            }
            return field
        }


    var contentString: String? = null
        get() {
            if (contentStringRes > 0) {
                return ResString.get(contentStringRes)
            }
            return field
        }

    @SuppressLint("ResourceAsColor")
    @ColorInt
    var contentColorInt: Int = R.color.common_text_black
        get() {
            if (field != 0) {
                return ContextCompat.getColor(context, field)
            }
            return field
        }

    @StringRes
    var titleStringRes: Int = 0
    var titleString: String? = null
        get() {
            if (titleStringRes > 0) {
                return ResString.get(titleStringRes)
            }
            return field
        }

    @SuppressLint("ResourceAsColor")
    @ColorInt
    var titleColorInt: Int = R.color.common_text_black
        get() {
            if (field != 0) {
                return ContextCompat.getColor(context, field)
            }
            return field
        }

    fun leftOnClickListener(v: View) {
        leftClick.invoke()
        cancelSureDialog.dismiss()
    }

    fun rightOnClickListener(v: View) {
        rightClick.invoke()
        cancelSureDialog.dismiss()
    }


    fun build(): CancelSureDialog {
        return cancelSureDialog
    }

    fun show() {
        build().show()
    }

}
